from multiprocessing import Process,JoinableQueue as Queue
import time
# def func(num=0):
#     print(f"子进程输出: {num}")
# if __name__ == '__main__':
#     t1 = Process(target=func, args=('t1',))
#     t2 = Process(target=func)
#     t3 = Process(target=func,kwargs={'num':'t3'})
#     t1.start()
#     t2.start()
#     t3.start()
#     time.sleep(3)
#     print("主进程输出")
#----------------------------------------------------
#进程队列
# class Spider(object):
#     def __init__(self):
#         self.q = Queue()
#     def parse_put_job(self):
#         for day in range(1,51):
#             data = f"今天是第{day}天, 我依然爱你"
#             self.q.put(data)
#     def parse_get_job(self):
#         for day in range(1,51):
#             data = self.q.get()
#             print(data)
#     def run(self):
#         t1 = Process(target=self.parse_put_job())
#         t2 = Process(target=self.parse_get_job())
#         t1.start()
#         t2.start()
# if __name__ == '__main__':
#     s = Spider()
#     s.run()
#--------------------------------------------
#进程队列升级版
class Spider(object):
    def __init__(self):
        self.q = Queue()
    def parse_put_job(self):
        for day in range(1,51):
            data = f"今天是第{day}天, 我依然爱你"
            self.q.put(data)
            self.q.join()
            """
            三种方法
            添加完一个任务阻塞等待
            直到self.q.task_done()执行, 此处的阻塞释放执行下面代码
            """
        print("子进程t1执行结束")
    def parse_get_job(self):
        while True:
            data = self.q.get()
            self.q.task_done()
            """
            三种方法
            """
            print(data)
    def run(self):
        t1 = Process(target=self.parse_put_job)
        t2 = Process(target=self.parse_get_job)
        t1.start()
        #设置t2为守护进程
        t2.daemon = True
        t2.start()
        #阻塞主进程延缓主进程的结束时间
        """
        一种方法
        time.sleep(0.1)
        """
        """
        二种方法
        t1.join()
        """
        """
        三种方法
        """
        t1.join()
if __name__ == '__main__':
    s = Spider()
    s.run()
    print("主进程执行结束")